<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>	
<script type="text/javascript">

$.ajaxSetup ({
   cache: false //关闭AJAX相应的缓存
});

var isIe = $.browser.msie;
var valueFilterNum = 0;

//定义全局变量记录分页信息  
var paramData = {  
 page:1,  
 rows:20,
 sortOrder:'desc' 
};  
var msgOut={};

$(function(){
	//alert(11);
	
	datagrid.datagridObj.pagination=true;
	
	datagrid.datagridObj.onLoadSuccess=function(){ 
    	$('#'+datagrid.datagridId).datagrid('loaded'); 
    	//if(isIe){
    		//addRows();
    	//}
        $("#queryBtn_Filter").removeAttr("disabled"); 
	};
	datagrid.datagridObj.doPagination=function(pPageIndex, pPageSize){
    	paramData.page = pPageIndex;
    	paramData.rows = pPageSize; 
    	reLoadData(); 
	};
	$('#'+datagrid.datagridId).datagrid(datagrid.datagridObj);

	//$('#'+datagrid.datagridId).datagrid("addToolbarItem",[{"text":"xxx"}]);
	
	var pager=$('#'+datagrid.datagridId).datagrid('getPager');
	$(pager).pagination({
		pageSize:paramData.rows,
		pageList:[20,40,60,80],
		beforePageText: '第',//页数文本框前显示的汉字   
        afterPageText: '页    共 {pages} 页',   
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'/*,
        onBeforeRefresh:function(pageNumber, pageSize){                      
            $(this).pagination('loading');                       
            alert('pageNumber:'+pageNumber+',pageSize:'+pageSize+' total:{total}');                       
        	$(this).pagination('loaded');                    
        }*/
	});

	
	$("#queryBtn_Filter").click(function(){
		var sortColumn = $("#propertySortSelect_Filter").getSelectedValue();
		if(sortColumn=='0'){
			return ;
		}
		paramData.sortColumn = sortColumn;
		//paramData.page=1;
		
		reLoadData();  
	});
	$("#queryBtn_FilterTmp").click(function(){
		//addConditionFun();
		
		var sortColumn = $("#propertySortSelect_Filter").getSelectedValue();
		if(sortColumn=='0'){
			return ;
		}
		paramData.sortColumn = sortColumn;
		//paramData.page=1;
		
		reLoadData();  
	});
	

	$("#addConditionBtn_Filter").click(function(){
		addConditionFun();
	});

	$("#deleteSelected_Filter").click(function(){
		var property = $("#container_Filter").getSelectedValue();
		delete paramData[property];
		$("#container_Filter").removeSelected();
	});
	
	//load for property column
	for(var key in datagrid.datagridProperty){
		$("#propertySelect_Filter").addOption(datagrid.datagridProperty[key],key);
	} 
	
	//load for property sort column
	for(var key in datagrid.datagridPropertySort){
		$("#propertySortSelect_Filter").addOption(datagrid.datagridPropertySort[key],key);
	} 


	$('#valueDate').hide();
	$('#valueSelect').hide();
});

function addConditionFun(){
	var property = $("#propertySelect_Filter").getSelectedValue();
	if(property=='0')return;
	if(property==null)return;
	
	var text = $("#propertySelect_Filter").getSelectedText();
	if(text==null)return;
	if(text=='')return;

	if(valueFilterNum==1){
		var value1 = $("#value2_Filter").val();
		if(value1=='')return;
		var r = $("#container_Filter").addOptionInstead(text+':'+value1,property);
		if(!r)return;
		paramData[property]=value1;
		$("#value2_Filter").val('');
	}else if(valueFilterNum==2){
		var value1 = $("#value1_Filter").datebox('getValue');
		if(value1=='')return;
		var r = $("#container_Filter").addOptionInstead(text+':'+value1,property);
		if(!r)return;
		paramData[property]=value1;
		$("#value1_Filter").datebox('setValue','');
	}else if(valueFilterNum==3){
		var value2 = $("#valueSelectData").getSelectedValue();
		var text2 = $("#valueSelectData").getSelectedText();
		var r = $("#container_Filter").addOptionInstead(text+':'+text2,property);
		if(!r)return;
		paramData[property]=value2;
	}
	
}

function deleteSelectedItem(){
	var property = $("#container_Filter").getSelectedValue();
	delete paramData[property];
	$("#container_Filter").removeSelected();
}
	
	
var tt;
function addRows(){
	clearInterval(tt);
	var length = msgOut.rows.length;
	var i=0;
	tt = setInterval(function(){
		$('#'+datagrid.datagridId).datagrid("appendRow",msgOut.rows[i++]);
		if(i==length){
			clearInterval(tt);
		}
	},100);

}

function sortOrderFun(order){
	paramData.sortOrder=order;
}

function reLoadData(){
	//$('#queryBtn_Filter').val('处理中..');
	$("#queryBtn_Filter").attr({"disabled":"disabled"});

	$('#'+datagrid.datagridId).datagrid('loading');//打开等待div  
	$.ajax({
		type:"POST",
		url:datagrid.datagridUrl,
		dataType:"json",
		data:paramData,
		success:function(msg){
			/*if(isIe){
				msgOut = msg;
				var msgNew = {};
				msgNew.total=msg.total-msg.rows.length;
				msgNew.rows=[];
				msgNew.footer=msg.footer;
				$('#'+datagrid.datagridId).datagrid('loadData',msgNew); 
			}else*/{
				$('#'+datagrid.datagridId).datagrid('loadData',msg); 
			}
		},
		error:function(er){

			//alert(er.responseText.trim());
			if(er.responseText!='' && er.responseText.indexOf('date error')!=-1){
				alert(er.responseText);
			}else{
				if(er.responseText!='' && er.responseText.indexOf('资源权限未分配')!=-1){
					alert('资源权限未分配');
				}
				if(er.responseText!='' && er.responseText.indexOf('登录')!=-1){
					alert('会话超时，请重新登录');
				}else{
					alert(er.responseText.trim());
					//alert('系统出错或无操作权限');
				}
			}
	        $("#queryBtn_Filter").removeAttr("disabled"); 
	        $('#'+datagrid.datagridId).datagrid('loaded');
		}
	})
	
}

//
function propertyMultiQuery(o){
	if(datagrid.datagridPropertyMultiQuery === undefined){
		if(o.value.indexOf('Date')!=-1 || o.value.indexOf('Time')!=-1){
			$('#valueDate').show();
			$('#valueInput').hide();
			valueFilterNum = 2;
		}else{
			$('#valueDate').hide();
			$('#valueInput').show();
			valueFilterNum = 1;
		}
		$('#valueSelect').hide();
	}else{
		var ccc = 0;
		for(var key in datagrid.datagridPropertyMultiQuery){
			ccc = ccc+1;
			if(key==o.value){
				if($('#'+key).get(0) != null){
					$('#valueDate').hide();//20130424
					$('#valueInput').hide();
					$('#valueSelect').show();
					valueFilterNum = 3;
					$('#valueSelectData').clearAll();
					
					var opts = $('#'+key).get(0).options;
					var len = opts.length;
					var i;
					for(i=0;i<len;i++){
						$('#valueSelectData').addOption(
								opts[i].text,
								opts[i].value);
					}
					break;
				}
				
			}else{
				//alert(o.value.indexOf('Date'));
				if(o.value.indexOf('Date')!=-1 || o.value.indexOf('Time')!=-1){
					$('#valueDate').show();
					$('#valueInput').hide();
					valueFilterNum = 2;
				}else{
					$('#valueDate').hide();
					$('#valueInput').show();
					valueFilterNum = 1;
				}
				$('#valueSelect').hide();
			}
		} 
		//20150208 datagrid.datagridPropertyMultiQuery 中没有对象时执行
		if(ccc==0){
			if(o.value.indexOf('Date')!=-1 || o.value.indexOf('Time')!=-1){
				$('#valueDate').show();
				$('#valueInput').hide();
				valueFilterNum = 2;
			}else{
				$('#valueDate').hide();
				$('#valueInput').show();
				valueFilterNum = 1;
			}
			$('#valueSelect').hide();
		}
		
	}

}

$(function(){
	var interval;
	function run(intervalTime){
		interval = setInterval(reLoadData,intervalTime*1000);
	}

	$('#pauseReloadData').click(function(){
		alert('暂停自动刷新功能成功');
		clearTimeout(interval);
	});
	
	$('#startReloadData').click(function(){
		var intervalTime = $('#intervalTime').val();
		alert('启动自动刷新功能成功,间隔:'+intervalTime+'秒');
		clearTimeout(interval);
		run(intervalTime);
	});
});

/* ie不执行，页面不显示
$(window).resize(function(){
	$('#'+datagrid.datagridId).datagrid('resize', {
		width:function(){return document.body.clientWidth;},
		height:function(){return document.body.clientHeight;},
	});
});
*/
</script>
</head>

<body>
<table class="table1" align="center" width="200px" cellpadding="0" cellspacing="0">
	<thead>
		<tr style="background-color: #efefef; color: #15428B;">
			<td colspan="2" align="center">
				<strong>查询条件</strong>
				<input type="hidden" name="flagForUse">
			</td>
		</tr>
	</thead> 
	<tr>
		<td>
			属性:
		</td>
		<td>
			<select id="propertySelect_Filter" onchange="javascript:propertyMultiQuery(this);">
				<option value="0">-请选择-</option>
			</select>
		</td>
	</tr>
	
	<tr id="valueInput">
		<td>
			值:
		</td>
		<td>
			<input size="11" name="value2_Filter" id="value2_Filter" >
		</td>
	</tr>
	<tr id="valueDate">
		<td>
			值:
		</td>
		<td>
			<input size="11" name="value1_Filter" class="easyui-datebox" id="value1_Filter" >
		</td>
	</tr>
	<tr id="valueSelect">
		<td>
			值:
		</td>
		<td>
			<select id="valueSelectData" style="width: 120px;"></select>
		</td>
	</tr>
	
	<tr>
		<td colspan="2"> 
			<a id="addConditionBtn_Filter" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" >添加查询条件</a>
			<a id="queryBtn_FilterTmp" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'" >查询</a>
		</td> 
	</tr>
	<tr style="background-color: #efefef; color: #15428B;">
			<td colspan="2" align="center" style=" border-top: 1px solid #99BBE8;">
				<strong>查询条件框</strong>
			</td>
	</tr>
	<tr>
		<td colspan="2">
			<select id="container_Filter" size="15" style="width: 170px;" title="双击删除选中项" ondblclick="javascript:deleteSelectedItem();">
			</select>
		</td>
	</tr>
	<tr>
		<td> 
			排序:
		</td>
		<td> 
			<select id="propertySortSelect_Filter" 
				name="sortColumn"
				>
				<!-- <option value="0">-请选择-</option> -->
			</select>
		</td> 
	</tr>
	<tr>
		<td> 
			&nbsp;
		</td>
		<td> 
			<input name="sortOrder" value="asc" type="radio"  onclick="javascript:sortOrderFun('asc');">
			升序
			<input name="sortOrder" value=desc type="radio" onclick="javascript:sortOrderFun('desc');" checked="checked">
			降序
		</td> 
	</tr>
	<tr>
		<td colspan="2">
			<a id="deleteSelected_Filter" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-cancel'">删除选中项</a>
			<a id="queryBtn_Filter" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'" >查询</a>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<a id="startReloadData" href="#" class="easyui-linkbutton" data-options="plain:true" >启动刷新</a>
			<a id="pauseReloadData" href="#" class="easyui-linkbutton" data-options="plain:true">暂停刷新</a>
			<input id="intervalTime" type="text" size="1" value="5" style="width: 20px; text-align: center;">秒
		</td>
	</tr>
	
</table>
</body>
</html>